﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class AttachmentBll
    {
        public AttachmentDal attachmentDal { get; set; }

        public ILog log { get; set; }

        public ResResult resResult { get; set; }

        /// <summary>
        /// 根据outerid获取附件列表
        /// </summary>
        /// <param name="outerid"></param>
        /// <returns></returns>
        public IEnumerable<AttachmentView> FindTAttachments(string outerid) {
    
            try
            {
               return attachmentDal.FindTAttachments(outerid);
               
            }
            catch (Exception ex)
            {
                log.Error("FindTAttachments: " + ex.Message);
                return null;
            }
        }

     
        /// <summary>
        /// 更新附件状态
        /// </summary>
        /// <param name="attid"></param>
        /// <returns></returns>
        public ResResult ModAttState(string attid) {
           
            try {
                if (string.IsNullOrWhiteSpace(attid)) {
                    log.Error("ModAttState 出现错误：传入id为空");
                    return resResult.FailResult("AttachmentBll ->ModAttState : 传入实体为空");
                }
                if (attachmentDal.FindTAttachmentByAttid(attid)==null) {
                    return resResult.FailResult("AttachmentBll ->ModAttState : 该附件信息不存在");
                }
                return resResult.SuccessResult(attachmentDal.ModAttState(attid));
            } catch (Exception e) {
                log.Error("ModAttState 出现错误："+e.Message);
                return resResult.FailResult("AttachmentBll ->ModAttState Error: "+e.Message);
            }
            
        }

        /// <summary>
        /// 更新附件状态
        /// </summary>
        /// <param name="attid"></param>
        /// <returns></returns>
        public ResResult ModAttStateByDir(string dir)
        {

            try
            {
                if (string.IsNullOrWhiteSpace(dir))
                {
                    log.Error("ModAttStateByDir 出现错误：传入路径为空");
                    return resResult.FailResult("AttachmentBll ->ModAttStateByDir : 传入路径为空");
                }
               
                return resResult.SuccessResult(attachmentDal.ModAttStateByDir(dir));
            }
            catch (Exception e)
            {
                log.Error("ModAttStateByDir 出现错误：" + e.Message);
                return resResult.FailResult("AttachmentBll ->ModAttStateByDir Error: " + e.Message);
            }

        }

        /// <summary>
        /// 添加附件save(TAttachment attachment)
        /// </summary>
        /// <param name="attachmentView"></param>
        /// <returns></returns>
        public ResResult SaveAttachment(string attid,string imgName,Guid ownerid,Guid screenid) {
            try
            {
                AttachmentView attachmentView = new AttachmentView();
                attachmentView.ATTID = attid;
                attachmentView.ATTDIR = imgName.Replace("\"",string.Empty);
                attachmentView.OUTERID = screenid.ToString();
                attachmentView.MODUSER = ownerid.ToString();
                attachmentView.INUSER = ownerid.ToString();
                attachmentView.INTIME = DateTime.Now;
                attachmentView.MODTIME = DateTime.Now;
                attachmentView.ATTTYPE = "4201";
                attachmentView.ATTSTATE = 1;
                attachmentDal.SaveAttachment(attachmentView);
                return resResult.SuccessResult();
            }
            catch (Exception e)
            {
                log.Error("SaveAttachment 出现错误：" + e.Message);
                return resResult.FailResult("AttachmentBll ->SaveAttachment Error: " + e.Message);
            }

           
        }

    }
}
